' Note: an idea I had to run this app in the background and write entries to RunOnce every few minutes - this really won't work:
' http://www.microsoft.com/technet/technetmag/issues/2006/12/WindowsConfidential
' "When you try to install a service pack or upgrade to a newer version of Windows, the setup wizard checks the RunOnce key. If it's not empty, you will get an error message that says, "Hey, you still have some unfinished program installation in progress. Please finish that before you try to upgrade the operating system.""

''' Upon initialization, this Tool enumerates all processes, then removes those that don't have a visible Window and those not started by the current User.
''' The Tool will then enumerate all files held open by the User's visible applications.
''' The Tool presents a Dialog showing the User's applications as well as the documents held open by those applications.
''' The Tool will note any applications (and documents) selected by the User, and write entries to the User's RunOnce registry key for each application (and/or document).
''' 
Public Class UserConfiguration

#Region "Variables"

    Friend Const HKCURUNONCE As String = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce"
    Friend Const HKCUEXPLORER As String = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced"

    ''' <summary>
    ''' This collection lists all those apps that will be hidden from the user when the user is presented with the list of applications from which the user may choose to cache.
    ''' </summary>
    ''' <remarks>
    ''' Each entry must be specified with the same spelling as is represented by Process.ProcessName.
    ''' </remarks>
    Friend Shared HiddenApps As Hashtable

#End Region

#Region "Properties"

    Friend Shared displayNonVisibleWindowsValue As Boolean
    ' This Property stores the state of the user configuration (from Tools, Options menu) to enable the display of non-visible Windows.
    Public Shared ReadOnly Property DisplayNonVisibleWindows() As Boolean
        Get
            Return displayNonVisibleWindowsValue
        End Get
    End Property

    Private Shared restoreExplorerWindowsValue As Boolean
    ' This Property stores the state of the user's configuration of Windows Explorer's setting to "restore previous folder windows at next logon"
    Public Shared Property RestoreExplorerWindows() As Boolean
        Get
            Return restoreExplorerWindowsValue
        End Get
        Set(ByVal value As Boolean)
            restoreExplorerWindowsValue = value
            my.Computer.Registry.SetValue(HKCUEXPLORER, "PersistBrowsers", Convert.ToInt32(value))
        End Set
    End Property

#End Region

#Region "Public Methods"

    ''' <summary>
    ''' Initialize the current user's configuration settings.
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared Sub GetAppConfig()
        GetUserConfiguration()
    End Sub

    ''' <summary>
    ''' Save the current user's configuration settings.
    ''' </summary>
    ''' <remarks></remarks>
    Public Shared Sub SetAppConfig()
        SetUserConfiguration()
    End Sub

#End Region

#Region "Private Methods"

    ''' <summary>
    ''' Read in user configuration from application settings.
    ''' </summary>
    ''' <remarks>
    ''' Currently the application uses the user.config application settings file.
    ''' </remarks>
    Private Shared Sub GetUserConfiguration()
        ' The setting that enables the display of non-visible Windows apps in the Apps checklist will toggle the internal variable to match
        If My.MySettings.Default.DisplayInvisibleWindows Then
            displayNonVisibleWindowsValue = True

        Else : displayNonVisibleWindowsValue = False

        End If

        HiddenApps = New Hashtable() ' instantiate the variable

        ' Enumerate the strings from the HiddenApps setting (a Collection) that represent the default and/or user-selected apps that should be hidden from the checklist of potential apps to cache
        For Each _ProcessName As String In My.Settings.HiddenApps
            ' 2009-11-15: Changed this variable to HashTable datatype, and for the moment I'm passing in _ProcessName as both needed parameters
            HiddenApps.Add(_ProcessName, _ProcessName)
        Next

        ' Read in the user's Registry settings
        ' This setting configures Windows Explorer to enable "restore folder windows at next logon"
        If My.Computer.Registry.GetValue(HKCUEXPLORER, "PersistBrowsers", 0) = 1 Then
            RestoreExplorerWindows = True
        Else : RestoreExplorerWindows = False
        End If

    End Sub

    ''' <summary>
    ''' Save the current membership of the HiddenApps collection to user settings.
    ''' </summary>
    ''' <remarks></remarks>
    Private Shared Sub SetUserConfiguration()

        Dim PreviousHiddenApps As Collection
        PreviousHiddenApps = New Collection

        ' First cache the values last found in the user's HiddenApps settings in user.config
        For Each _ProcessName As String In My.Settings.HiddenApps
            PreviousHiddenApps.Add(_ProcessName)
        Next

        ' Next remove the current user settings
        My.Settings.HiddenApps.Clear()

        For Each _ProcessName As String In HiddenApps
            My.Settings.HiddenApps.Add(_ProcessName)
        Next

        My.Settings.Save()

        ' TODO: create logic to restore the previous settings if an unrecoverable error is encountered (including throwing an Exception that can be propagated to the user)
        ' TODO: dispose of the temporary cache of previously hidden applications

    End Sub

#End Region

End Class
